package UI;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.GregorianCalendar;
import java.util.Scanner;
import java.util.concurrent.BlockingQueue;

import Debug.Debug;
import Enums.Type_rsp;
import Interfaces.C_interface;
import Interfaces.S_interface;
import Structs.Member;
import Structs.Msg_rsp;
import Threads.C_RMI;
import Threads.S_RMI;

public class Client_Drop_RMI {

	private static Integer RMI_port=7000,opt,id;
	private static String User, Password,requests,title;
	private static Type_rsp Type = null;
	private static boolean loged;
	private static Member client = new Member("","");
	private static BufferedReader reader;
	private static BlockingQueue<Msg_rsp> Requests;
	
	public static void main(String[] args) throws IOException  {
		
		System.out.println("------------------");
		System.out.println("Welcome to MyDrop");
		System.out.println("------------------");
		
		try {
			
			S_interface serv = (S_interface) LocateRegistry.getRegistry(RMI_port).lookup("MyDrop");
			C_RMI cl = new C_RMI(RMI_port, Requests, serv,client);
			
			reader = new BufferedReader(new InputStreamReader(System.in));
			
			
			
			do{
				System.out.println("Choose command:\n1.Login\n2.Register\n3.Exit");
				opt = Integer.parseInt(reader.readLine());
				
				
				switch(opt){
				case 1 :
					
					System.out.println("Insert Username:");
					User = reader.readLine();
					
					System.out.println("Insert Password:");
					Password = reader.readLine();
					
					if(serv.Login(User,Password, cl)){
						client.setUser(User);
						client.setPass(Password);
						System.out.println("Welcome!");
						taskmanager(serv);
					}
					else{
						System.out.println("Wrong user/password combination");
					}
					
					break;
				
				case 2:
					
					System.out.println("New Insert Username:");
					User = reader.readLine();
					
					System.out.println("New Insert Password:");
					Password = reader.readLine(); 
					
					if(serv.Register(User, Password)){
						serv.Login(User, Password, cl);
						client.setUser(User);
						client.setPass(Password);
						System.out.println("Welcome");
						taskmanager(serv);
					}
					else
						System.out.println("User already in System!");
					
				break;
				
				case 3:
					System.out.println("Bye Bye");
					
				break;
				}
				
			}while(opt!=3);
			
		} catch (MalformedURLException e) {
			
			if(new Debug().isDebug())
				System.out.println("URL Malform Client Drop");
			
		} catch (RemoteException e) {
			
			if(new Debug().isDebug()){
				System.out.println("Remote exception Client Drop");
				e.printStackTrace();
			}
			
		} catch (NotBoundException e) {
			
			if(new Debug().isDebug())
				System.out.println("Not Bound Exception Client Drop");
			
		}
		
	}
	
	public static void taskmanager(S_interface serv) throws IOException{
		
		do{
		try{
			
				System.out.println("Select Command:");
				requests = reader.readLine();
				Type = Type_rsp.valueOf(requests);
				
				switch (Type) {
				
				case NewTask:
					System.out.println("Select Name for the task:");
					title=reader.readLine();
					serv.addTask(title,client,(GregorianCalendar) GregorianCalendar.getInstance());
					break;
					
				case EditTask:
					
					System.out.println("Insert ID to edit:");
					id = Integer.parseInt(reader.readLine());
					System.out.println("Insert the new Name:");
					title =reader.readLine();
					serv.addTask(id, title, client, (GregorianCalendar) GregorianCalendar.getInstance());				
					break;
				
				case FinishTask:
					System.out.println("Insert ID to finish:");
					id = Integer.parseInt(reader.readLine());
					serv.addTask(id, title, client, (GregorianCalendar) GregorianCalendar.getInstance());
				
				case ListTasks:
					serv.addTask("List", client, (GregorianCalendar) GregorianCalendar.getInstance());
				
				case Logout:	
					Type = Type_rsp.Logout;
					break;
				
				case Exit: 
					Type = Type_rsp.Exit;
					return;
				
				default: break;
				}
				
			
		
		}catch(IllegalArgumentException e){
			System.out.println("Comando inválido");
		}
		}while(Type != Type_rsp.Logout || Type != Type_rsp.Exit);
	}
}
